package ch.ethz.fcl.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Line extends View {
	float x1, x2, y1, y2;
	public Paint mpaint = new Paint(Paint.ANTI_ALIAS_FLAG);

	public Line(Context context, float x1, float y1, float x2, float y2) {
		super(context);
		mpaint.setColor(Color.BLUE);
		mpaint.setStrokeWidth(5.0f);
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawLine(x1, y1, x2, y2, mpaint);
	}
	
	public float midX(){
		return (x1 + x2) / 2.0f;
	}
	
	public float midY(){
		return (y1 + y2) / 2.0f;
	}
	
	public void drag(float xDrag, float yDrag){
		this.x1 += xDrag;
		this.x2 += xDrag;
		this.y1 += yDrag;
		this.y2 += yDrag;
	}
	
	public void compress(){
		float xdiff = Math.abs(x1 - x2);
		float ydiff = Math.abs(y1 - y2);
		
		if(x1 > x2){
			x1 -= 0.05f * xdiff;
			x2 += 0.05f * xdiff;
		}
		else{
			x1 += 0.05f * xdiff;
			x2 -= 0.05f * xdiff;
		}
		
		if(y1 > y2){
			y1 -= 0.05f * ydiff;
			y2 += 0.05f * ydiff;
		}
		else{
			y1 += 0.05f * ydiff;
			y2 -= 0.05f * ydiff;
		}	
	}
	
	public void extend(){
		float xdiff = Math.abs(x1 - x2);
		float ydiff = Math.abs(y1 - y2);
		
		if(x1 > x2){
			x1 += 0.05f * xdiff;
			x2 -= 0.05f * xdiff;
		}
		else{
			x1 -= 0.05f * xdiff;
			x2 += 0.05f * xdiff;
		}
		
		if(y1 > y2){
			y1 += 0.05f * ydiff;
			y2 -= 0.05f * ydiff;
		}
		else{
			y1 -= 0.05f * ydiff;
			y2 += 0.05f * ydiff;
		}	
	}

	public void rotate(double delta){
		double sin = Math.sin(delta);
		double cos = Math.cos(delta);
		
		float centerX = (x1 + x2) / 2;
		float centerY = (y1 + y2) / 2;
		
		x1 = (float)(cos * x1 - sin * y1 - centerX * cos + centerY * sin + centerX);
		y1 = (float)(sin * x1 + cos * y1 - centerX * sin - centerY * cos + centerY);
		x2 = (float)(cos * x2 - sin * y2 - centerX * cos + centerY * sin + centerX);
		y2 = (float)(sin * x2 + cos * y2 - centerX * sin - centerY * cos + centerY);
	}
	
	public String info() {
		return Bounds.getBoundX(x1) + ":" + Bounds.getBoundY(y1) + ":"
				+ Bounds.getBoundX(x2) + ":" + Bounds.getBoundY(y2);
	}
}
